let path = getDataPath();
// 用户ID
let id = getUrlParam("id");
$(function () {
    // GetUserRole();
    loadFnc();
});
//获取用户权限
function GetUserRole() {
    // let thisdata = [];
    // thisdata.push(InitRole(id));
    let USERdata = InitRole(id);
    if (USERdata != null) {
        loadFnc();
    }
    else OpenErrmsg("web/errhtml/404.html");
}
function loadFnc() {
    $('#db_Start').datebox('setValue', GetDateStr(-13));
    $('#db_End').datebox('setValue', getCurrDate());
    searchData();
}

function searchData() {
    let StartDate = $('#db_Start').datebox('getValue');
    let EndDate = $('#db_End').datebox('getValue');
    let SearchType = $('#cmb_SearchType').textbox('getValue');
    let daycount = DateDiff(StartDate, EndDate);
    if (StartDate != "" && EndDate != "" && daycount > 14) {
        $.messager.alert('警告', '查询日期不能超过两周！');
    } else {
        // InitChartYSJ_A();
        InitData(StartDate, EndDate, SearchType);
        InitData2(StartDate, EndDate, SearchType);
        InitData3(StartDate, EndDate, SearchType);
        InitChart(StartDate, EndDate, SearchType);
    }
}
function InitData(StartDate, EndDate, SearchType) {
    let url = path + '/ls172/data-analysis/makeReport'
    var params = {
        "data": SearchType,
        "endTime": EndDate,
        "startTime": StartDate
      }
    $.ajax({
        url: url,
        type: "post",
        async: false,
        dataType: "json",
        headers: {
            Accept: "application/json",
            "Content-Type": "application/json"
        },
        data: JSON.stringify(params),

        success: function (data) {
            total = data.data.faultList.length
            $('#dg_DateList').datagrid({
                data: data.data.faultList,
                height: 32 * (total + 1) + 10,
            });
            document.getElementById('sp_Date').innerText = StartDate + ' 至 ' + EndDate
            document.getElementById('sp_concent').innerText = SearchType
        },
        error: function (data) {
            console.log("code", data);
        }
    })
}
function InitData2(StartDate, EndDate, SearchType) {

    let columns = [];
    //后台返回列头
    let dataTitle = [
        { "ID": "time", "name": "日期" },
        { "ID": "row1", "name": "2021.02.13 11:19:15" },
        { "ID": "row2", "name": "2021.02.13 11:19:30" },
        { "ID": "row3", "name": "2021.02.13 11:19:45" },
        { "ID": "row4", "name": "2021.02.13 11:20:00" },
        { "ID": "row5", "name": "2021.02.13 11:20:15" },
        { "ID": "row6", "name": "2021.02.13 11:20:30" },
        { "ID": "row7", "name": "2021.02.13 11:20:45" },
    ];
    let totalnum = 0
    for (let index = 0; index < dataTitle.length; index++) {
        const element = dataTitle[index];
        totalnum++
        columns.push({ field: element.ID, title: element.name, width: 180 });
    }
    for (let index = 0; index < columns.length; index++) {
        columns[index].width = (window.innerWidth - 30)/totalnum
    }
    //后台返回数据
    let data = {
        "total": 1, "rows": [
            { "time": "一级压缩机A出口温度", "row1": "67", "row2": "61", "row3": "39", "row4": "65", "row5": "34", "row6": "10", "row7": "90" },
        ]
    };
    //加载数据
    $('#dg_DateList2').datagrid({
        data: data,
        columns: [columns],
        height: 35 * (data.total + 1) + 10,
    });
}
function InitData3(StartDate, EndDate, SearchType) {
    let totalnum = 0
    let columns = [];
    //后台返回列头
    let dataTitle = [
        { "ID": "time", "name": "日期" },
        { "ID": "row1", "name": "2021.02.13 11:19:15" },
        { "ID": "row2", "name": "2021.02.13 11:19:30" },
        { "ID": "row3", "name": "2021.02.13 11:19:45" },
        { "ID": "row4", "name": "2021.02.13 11:20:00" },
        { "ID": "row5", "name": "2021.02.13 11:20:15" },
        { "ID": "row6", "name": "2021.02.13 11:20:30" },
        { "ID": "row7", "name": "2021.02.13 11:20:45" },
    ];
    for (let index = 0; index < dataTitle.length; index++) {
        const element = dataTitle[index];
        totalnum++
        columns.push({ field: element.ID, title: element.name, width: 180 });
    }
    for (let index = 0; index < columns.length; index++) {
        columns[index].width = (window.innerWidth - 30)/totalnum
    }
    //后台返回数据
    let data = {
        "total": 1, "rows": [
            { "time": "二级压缩机B出口温度", "row1": "67", "row2": "61", "row3": "39", "row4": "65", "row5": "34", "row6": "10", "row7": "90" },
        ]
    };
    //加载数据
    $('#dg_DateList3').datagrid({
        data: data,
        columns: [columns],
        height: 35 * (data.total + 1) + 10,
    });
}
function InitChart(StartDate, EndDate, SearchType) {
    InitChart1();
    InitChart2();
}
//一级压缩机A出口温度
function InitChart1() {
    let xAxis = ["11:40:00", "11:40:15", "11:40:30", "11:40:45", "11:41:00"];
    let data = [
        { "code": "01", "name": "压力", "data": ["34", "67", "53", "54", "132"] },
        { "code": "02", "name": "低阙值", "data": ["345", "17", "153", "54", "232"] },
        { "code": "02", "name": "高阙值", "data": ["15", "167", "153", "564", "22"] },
    ];
    createChart("chartdiv1", xAxis, data, "一级压缩机A出口温度");
}
//二级压缩机B出口温度
function InitChart2() {
    let xAxis = ["11:40:00", "11:40:15", "11:40:30", "11:40:45", "11:41:00"];
    let data = [
        { "code": "01", "name": "温度", "data": ["334", "37", "533", "54", "132"] },
        { "code": "02", "name": "低阙值", "data": ["145", "167", "53", "54", "232"] },
        { "code": "02", "name": "高阙值", "data": ["345", "17", "153", "564", "32"] },
    ];
    createChart("chartdiv2", xAxis, data, "二级压缩机B出口温度");
}
function createChart(id, xAxis, data, title) {
    let myChart = echarts.init(document.getElementById(id));
    let xAxisDataLine = new Array();
    let seriesDataLine = new Array();
    for (let i = 0; i < data.length; i++) {
        let item = data[i];
        xAxisDataLine.push(item.name)
        seriesDataLine.push({
            name: item.name,
            type: "line",
            // stack: 'Total',
            data: item.data
        });
    }
    // console.log(xAxisDataLine)
    // 指定图表的配置项和数据
    let option = {
        title: {
            text: title,
            padding: 15,
            textStyle: {
                fontSize: 12,
                color: '#333'          // 主标题文字颜色
            },
        },
        tooltip: {
            trigger: 'axis'
        },
        legend: {
            data: xAxisDataLine,

        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        toolbox: {
            // feature: {
            //     saveAsImage: {}
            // }
        },
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data: xAxis
        },
        yAxis: {
            type: 'value'
        },
        series: seriesDataLine
    };
    // 使用刚指定的配置项和数据显示图表。
    myChart.setOption(option);
}